 package com.hongdee.poi;

 import org.apache.commons.beanutils.PropertyUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;

 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;

 public class ExcelWorkSheetHandler<T>
   implements SheetContentsHandler
 {
   private static final Log LOG = LogFactory.getLog(ExcelWorkSheetHandler.class);
   private static final String HEADER_KEY = "HEADER";
   private boolean verifiyHeader = true;
   private int skipRows = 0;
   private int HEADER_ROW = 0;
   private int currentRow = 0;
   private List<T> valueList;
   private Class<T> type;
   private Map<String, String> cellMapping = null;
   private T objCurrentRow = null;
   private T objHeader = null;

   public ExcelWorkSheetHandler(Class<T> type, Map<String, String> cellMapping)
   {
     this.type = type;
     this.cellMapping = cellMapping;
     this.valueList = new ArrayList();
   }

   public ExcelWorkSheetHandler(Class<T> type, Map<String, String> cellMapping, int skipRows)
   {
     this.type = type;
     this.cellMapping = cellMapping;
     this.valueList = new ArrayList();
     this.skipRows = skipRows;
   }

   public List<T> getValueList()
   {
     return this.valueList;
   }

   public boolean isVerifiyHeader()
   {
     return this.verifiyHeader;
   }

   public void setVerifiyHeader(boolean verifiyHeader)
   {
     this.verifiyHeader = verifiyHeader;
   }

   public void startRow(int rowNum)
   {
     this.currentRow = rowNum;

     if (this.verifiyHeader) {
       this.objHeader = getInstance();
     }

     if ((rowNum > this.HEADER_ROW) && (rowNum >= this.skipRows))
       this.objCurrentRow = getInstance();
   }

   public void cell(String cellReference, String formattedValue)
   {
     if (this.currentRow >= this.skipRows) {
       if (StringUtils.isBlank(formattedValue)) {
         return;
       }

       if ((this.HEADER_ROW == this.currentRow) && (this.verifiyHeader)) {
         assignValue(this.objHeader, getCellReference(cellReference), formattedValue);
       }

       assignValue(this.objCurrentRow, getCellReference(cellReference), formattedValue);
     }
   }

   public void endRow()
   {
     if ((this.HEADER_ROW == this.currentRow) && (this.verifiyHeader) && (this.objHeader != null) &&
       (!(checkHeaderValues(this.objHeader)))) {
       throw new RuntimeException("Header values doesn't match, so invalid Excel file!");
     }

     if (this.currentRow >= this.skipRows) {
       if ((this.objCurrentRow != null) && (isObjectHasValue(this.objCurrentRow)))
       {
         this.valueList.add(this.objCurrentRow);
       }

       this.objCurrentRow = null;
     }
   }

   public void headerFooter(String text, boolean isHeader, String tagName)
   {
   }

   private String getCellReference(String cellReference)
   {
     if (StringUtils.isBlank(cellReference)) {
       return "";
     }

     return cellReference.split("[0-9]*$")[0];
   }

   private void assignValue(Object targetObj, String cellReference, String value) {
     if ((targetObj == null) || (StringUtils.isEmpty(cellReference)) || (StringUtils.isEmpty(value))) {
       return;
     }
     try
     {
       String propertyName = (String)this.cellMapping.get(cellReference);
       if (propertyName == null) {
         LOG.error("Cell mapping doesn't exists!"); return;
       }
       PropertyUtils.setSimpleProperty(targetObj, propertyName, value);
     }
     catch (IllegalAccessException iae) {
       LOG.error(iae.getMessage());
     } catch (InvocationTargetException ite) {
       LOG.error(ite.getMessage());
     } catch (NoSuchMethodException nsme) {
       LOG.error(nsme.getMessage());
     }
   }

   private T getInstance() {
     try {
       return this.type.newInstance();
     } catch (InstantiationException ie) {
       LOG.error(ie.getMessage());
     } catch (IllegalAccessException iae) {
       LOG.error(iae.getMessage());
     }
     return null;
   }

   private boolean isObjectHasValue(Object targetObj)
   {
     for (Entry entry : this.cellMapping.entrySet()) {
       if ((!(StringUtils.equalsIgnoreCase("HEADER", (String)entry.getKey()))) &&
         (StringUtils.isNotBlank(getPropertyValue(targetObj, (String)entry.getValue())))) {
         return true;
       }
     }

     return false;
   }

   private boolean checkHeaderValues(Object targetObj) {
     boolean compareSuccess = true;
     if (this.cellMapping.containsKey("HEADER")) {
       List valueToCheck = Arrays.asList(((String)this.cellMapping.get("HEADER")).split(","));

       for (Entry entry : this.cellMapping.entrySet())
         if (!(StringUtils.equalsIgnoreCase("HEADER", (String)entry.getKey()))) {
           String value = getPropertyValue(targetObj, (String)entry.getValue());
           LOG.debug("Comparing header value from excel file: " + value);
           if (!(valueToCheck.contains(value))) {
             compareSuccess = false;
             break ;
           }
         }
     }
     else {
       LOG.warn("HEADER_KEY doesn't exists");
     }
     return compareSuccess;
   }

   private String getPropertyValue(Object targetObj, String propertyName) {
     String value = "";
     if ((targetObj == null) || (StringUtils.isBlank(propertyName))) {
       LOG.error("targetObj or propertyName is null, both require to retrieve a value");
       return value;
     }
     try
     {
       if (PropertyUtils.isReadable(targetObj, propertyName)) {
         Object v = PropertyUtils.getSimpleProperty(targetObj, propertyName);
         if ((v != null) && (StringUtils.isNotBlank(v.toString()))) {
           value = v.toString();
           return value;
         }
       }
       LOG.error("Given property (" + propertyName + ") is not readable!");
     }
     catch (IllegalAccessException iae) {
       LOG.error(iae.getMessage());
     } catch (InvocationTargetException ite) {
       LOG.error(ite.getMessage());
     } catch (NoSuchMethodException nsme) {
       LOG.error(nsme.getMessage());
     }
      return value;
   }
 }

/* Location:           D:\Documents\Downloads\classes\classes\
 * Qualified Name:     com.myjeeva.poi.ExcelWorkSheetHandler
 * JD-Core Version:    0.5.3
 */